var curPageIdx = 0;

var indexJson = new Array();
window.onload = function(){
	// 数据请求
     
	indexJson=[
	{'title': '《长生殿》一本【钗盒情定】', 'vedio': '../image/index/index-1.png', 'hot': '0'},
	{'title': '12月1日KPL秋季赛季后赛 第五场', 'vedio': '../image/index/index-1.png', 'hot': '0'},
	{'title': 'TOP5第31期 张大仙天秀貂蝉四杀', 'vedio': '../image/index/index-1.png', 'hot': '1'},
	{'title': 'TOP5第31期 张大仙天秀貂蝉四杀', 'vedio': '../image/index/index-1.png', 'hot': '0'},
	{'title': 'TOP5第31期 张大仙天秀貂蝉四杀', 'vedio': '../image/index/index-1.png', 'hot': '0'},

	{'title': '《长生殿》一本【钗盒情定】' ,'img': '../image/index/index-2.png'},
	{'title': '武汉开展“戏剧进校园”活动，让学生感受戏曲文化的魅力...' ,'img': '../image/index/index-2.png'},

	{'title': '花木兰' ,'img': '../image/index/index-3.png'},
	{'title': '戏曲演员电视大赛复赛第三场' ,'img': '../image/index/index-4.png'},
	{'title': '女驸马' ,'img': '../image/index/index-5.png'}

	]
 /* function loadData(){*/
  var vedioInner = '';
  var periodsInner = '';
  //var hotInner = '';
  var albumfInner = '';
  var albumsInner = '';
  for (var i = 0; i < indexJson.length; i++) {

    // if (i === 1){
    //   periodsInner += '<div ican="true" id="item_'+(i+1)+'" class="periods-item even"><div class="text">'+indexJson[i]['title']+'</div></div>';
    //   continue
    // }

    if (i <= 4) {
      if (indexJson[i]['hot'] == 0) {
        if (i % 2 == 0) {
          periodsInner += '<div ican="true" id="item_'+(i+1)+'" class="periods-item even"><div class="text">'+indexJson[i]['title']+'</div></div>';
        }else{
          periodsInner += '<div ican="true" id="item_'+(i+1)+'" class="periods-item odd"><div class="text">'+indexJson[i]['title']+'</div></div>';
        }
      }else{
        if (i % 2 == 0) {
          periodsInner += '<div ican="true" id="item_'+(i+1)+'" class="periods-item even"><div class="text">'+indexJson[i]['title']+'</div><div class="hot"><span>直播</span></div></div>';
        }else{
          periodsInner += '<div ican="true" id="item_'+(i+1)+'" class="periods-item odd"><div class="text">'+indexJson[i]['title']+'</div><div class="hot"><span>直播</span></div></div>';
        }
      }
    }else if (i < 7) {
      albumfInner += '<div ican="true" id="item_'+(i+1)+'" class="albumf" style="background-image: url('+indexJson[i]['img']+')"> <div class="cover">'+indexJson[i]['title']+'</div></div>';
    }else{
      albumsInner += '<div ican="true" id="item_'+(i+1)+'" class="albums" style="background-image: url('+indexJson[i]['img']+')"> <div class="cover">'+indexJson[i]['title']+'</div></div>';
    } 
  }
  //document.getElementById('left').innerHTML  +='<div class="video-item" style="background-image: url('+indexJson[0]['vedio']+');"></div>';
  //document.getElementById('periods').innerHTML = periodsInner;
  //document.getElementById('item_1').innerHTML = hotInner;
  document.getElementById('album-f').innerHTML = albumfInner;
  document.getElementById('album-s').innerHTML = albumsInner;
/*}*/

}
	
var headerUrls = ['../arena/arena.html','../famous-part/famous-part.html','../class/classDetail.html','../my/Record.html','../search/search.html'];

// document.onkeydown = function (ev) {
//   console.log(ev)
// }

// 设置初始状态
// document.onkeydown = focusEvent;
function focusEvent(e){
  // 获取 首 页 中的当前焦点的id
  var curActiveMatch;
  curActiveMatch = document.getElementsByClassName('sel');
  //header上方搜索图标
  var search = document.getElementsByClassName('search-acti');
  var favorite = document.getElementsByClassName('favorite-acti');
  var history = document.getElementsByClassName('history-acti');
  if (search.length != 0) {
  	curActiveMatch = search;
  }
  if (favorite.length != 0) {
  	curActiveMatch = favorite;
  }
  if (history.length != 0) {
  	curActiveMatch = history;
  }

  var curSelectedIdx;
  if (curActiveMatch.length != 0) {
  	console.log(JSON.stringify(curActiveMatch))
    curSelectedIdx = parseInt(curActiveMatch[0].id.replace('item_',''));	 
  }

  // 获取header中的li的当前焦点的index
  var curActiveLi = document.getElementsByClassName('header_selected_li');
  var curLiIdx = parseInt(curActiveLi[0].id.replace('header_li_',''));



  e = e || window.event; //这一行及下一行是为兼容IE8及以下版本
  var curWhichKey = e.which;
 // var isOk = 0;//后退

  switch(curWhichKey){
    case 13: // 回车/OK
      /*isOk = 1;*/
      /*switch(curLiIdx){
      	case 2:
      		window.location.href = headerUrls[0];
      		break;
      	case 3:
      		window.location.href = headerUrls[1];
      		break;
      	case 4:
      		window.location.href = headerUrls[2];
      		break;
      	case 5:
      		window.location.href = headerUrls[3];
      		break;
      }*/
      if (curSelectedIdx > 5 && curSelectedIdx < 12) {
        window.location.href = '../match/album_play.html';
      }
      if(search[0]){
      	if (search[0].id == 'search') {
	      	window.location.href = '../search/Search.html';
	    }
      }

      if (favorite[0]) {
      	if (favorite[0].id == 'favorite') {
	      	window.location.href = '../history/CollectItem.html';
	    }
      }
      
      if(history[0]){
      	if (history[0].id == 'history') {
	      	window.location.href = '../history/Record.html';
	      }
      }

      break;
    case 27: // ESC
    if (window.history.length>=1) {
      window.history.back();
      return false;
    }  
        break;
    case 37: // 左
      if (curActiveMatch.length == 0) { // 焦点在header
        if (curLiIdx != 1 && search.length != 0) {
          removeClass(getObjById('header_li_'+curLiIdx),'header_selected_li');
          removeClass(getObjById('header_li_'+curLiIdx),'header_only_foucused_li');
          addClass(getObjById('header_li_'+(curLiIdx-1)),'header_selected_li');
          addClass(getObjById('header_li_'+(curLiIdx-1)),'header_only_foucused_li');

        }
      }else{  // 焦点在首页
      	if(curSelectedIdx > 5){
      		if (curSelectedIdx == 6 || curSelectedIdx ==8 ) {
          		removeClass(getObjById('item_'+curSelectedIdx),'sel');
          		addClass(getObjById('item_'+1),'sel');
          		addClass(getObjById('video'),'acti');
        	}else{
        		if (curSelectedIdx == 7) {
        			removeClass(getObjById('item_'+curSelectedIdx),'sel');
            	addClass(getObjById('item_'+(curSelectedIdx-1)),'sel');
        		}else{
        			removeClass(getObjById('item_'+curSelectedIdx),'sel');
            	addClass(getObjById('item_'+(curSelectedIdx-1)),'sel');
        		}
          	}
    	}else{
    		/*if (search.length != 0) {
  				removeClass(getObjById('search'),'search-acti');
  			}*/
  			if (favorite.length != 0) {
  				removeClass(getObjById('favorite'),'favorite-acti');
          addClass(getObjById('search'),'search-acti');
  			}
  			if (history.length != 0) {
  				removeClass(getObjById('history'),'history-acti');
          addClass(getObjById('favorite'),'favorite-acti');
  			}
    	}  
      }
      break;

    case 38: // 上
      if (curActiveMatch.length == 0) {
      	addClass(getObjById('search'),'search-acti');
        removeClass(getObjById('header_li_'+(curLiIdx)),'header_selected_li');
        removeClass(getObjById('header_li_'+(curLiIdx)),'header_only_foucused_li');
        addClass(getObjById('header_li_1'),'header_selected_li');

      }else{
        // 焦点在左侧
        if (curSelectedIdx <= 5 && curSelectedIdx != 1) {
          removeClass(getObjById('item_'+curSelectedIdx),'sel');
          addClass(getObjById('item_'+(curSelectedIdx-1)),'sel'); 
        }else{ // 焦点在第一行
        	if (curSelectedIdx ==6 || curSelectedIdx==1 ||curSelectedIdx == 7) {
        		removeClass(getObjById('video'),'acti');
        		removeClass(getObjById('item_'+curSelectedIdx),'sel');
            addClass(getObjById('header_li_'+(curLiIdx)),'header_only_foucused_li');
        	}else{ // 焦点在第二行的
           			removeClass(getObjById('item_'+curSelectedIdx),'sel');
            		addClass(getObjById('item_'+6),'sel');
          		}
        	}
      }
      break;

    case 39: // 右
      if (curActiveMatch.length == 0) { // 焦点在header
        /*if (curLiIdx != 5) {
           removeClass(getObjById('header_li_'+curLiIdx),'header_selected_li');
           removeClass(getObjById('video'),'acti');
           removeClass(getObjById('header_li_'+curLiIdx),'header_only_foucused_li');
           addClass(getObjById('header_li_'+(curLiIdx+1)),'header_selected_li');
           addClass(getObjById('header_li_'+(curLiIdx+1)),'header_only_foucused_li');
        }*/
        window.location.href = '../arena/arena.html';
      }else{
      		console.log(curSelectedIdx)
          if (curSelectedIdx == 5) {
          	removeClass(getObjById('item_'+5),'sel');
          	removeClass(getObjById('item_'+5),'acti');
          	addClass(getObjById('item_'+6),'sel');
          	addClass(getObjById('item_'+6),'acti');
          }else if (curSelectedIdx == 6) {
          	removeClass(getObjById('item_'+curSelectedIdx),'sel');
         	  addClass(getObjById('item_'+(curSelectedIdx+1)),'sel');
          }else if (curSelectedIdx == 7 || curSelectedIdx == 8 || curSelectedIdx == 9) {
          	removeClass(getObjById('item_'+curSelectedIdx),'sel');
          	addClass(getObjById('item_'+(curSelectedIdx+1)),'sel');
          }else if (search.length != 0) {
  				  removeClass(getObjById('search'),'search-acti');
  				  addClass(getObjById('favorite'),'favorite-acti');
  		  }else if (favorite.length != 0) {
  				  removeClass(getObjById('favorite'),'favorite-acti');
            addClass(getObjById('history'),'history-acti');
  		  }else if (history.length != 0) {
  			}
        }
      
      break;
    
    case 40: // 下
      // 首次进入
      if (curActiveMatch.length == 0) {
        removeClass(getObjById('header_li_'+curLiIdx),'header_selected_li');
        removeClass(getObjById('header_li_'+curLiIdx),'header_only_foucused_li');
        addClass(getObjById('header_li_1'),'header_selected_li');
        addClass(getObjById('item_5'),'sel');
        addClass(getObjById('item_5'),'acti');
      }else{
        // 判断是第一行还是第二行
        if (curSelectedIdx < 5) {
//        	document.getElementById('left').innerHTML +='<div class="video-item" style="background-image: url('+indexJson[i]['vedio']+');"></div>';
          	removeClass(getObjById('item_'+curSelectedIdx),'sel');
          	addClass(getObjById('item_'+(curSelectedIdx+1)),'sel');
        }else{
        	 if (curSelectedIdx == 6 || curSelectedIdx == 7) {
        	 	removeClass(getObjById('video'),'acti');
          	removeClass(getObjById('item_'+curSelectedIdx),'sel');
          	addClass(getObjById('item_'+8),'sel');
        	}else if (search.length != 0 || favorite.length != 0 || history.length != 0) {
        		removeClass(getObjById('search'),'search-acti');
        		removeClass(getObjById('favorite'),'favorite-acti');
        		removeClass(getObjById('history'),'history-acti');
            addClass(getObjById('header_li_'+curLiIdx),'header_only_foucused_li');
        	}
        }
      }
      break;
  }
}

